package com.jason.apps.buyer;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;


import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.apps.essentials.ConsumeWebService;
import com.apps.realtyportal.LoginActivity;
import com.apps.realtyportal.R;
import com.jason.apps.alert.AlertDialogFragment;
import com.jason.apps.alert.EmptyAlert;
import com.jason.apps.alert.ErrorAlert;

public class EditMe extends SherlockFragment{

	public EditText fname;
	public EditText lname;
	public EditText email;
	public EditText mobile;
	public EditText landline;
	public EditText bday;
	public EditText username;
	public EditText password;
	public EditText filepath;
	public Button register;
	public Button browse;
	public Spinner type;
	public ImageButton cal;
	
	String host = "property-portal.uphero.com";
	String ftpuser = "a5596663";
	String ftppass = "ranger123";
	String saveLocation = "/public_html";
	String folderLocation = "/image/";
	String imagename;
	
	FTPClient con = null;
	
	SherlockFragmentActivity fa;
	ConsumeWebService consume=new ConsumeWebService();
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view=inflater.inflate(R.layout.lead_registration, container, false);
		fa = (SherlockFragmentActivity) super.getActivity();
		
		 fname=(EditText)view.findViewById(R.id.lead_fname);
		 lname=(EditText)view.findViewById(R.id.lead_lname);
		 email=(EditText)view.findViewById(R.id.lead_email);
		 mobile=(EditText)view.findViewById(R.id.lead_mobile);
		 landline=(EditText)view.findViewById(R.id.lead_landLine);
		 username=(EditText)view.findViewById(R.id.lead_username);
		 password=(EditText)view.findViewById(R.id.lead_password);
		 filepath=(EditText)view.findViewById(R.id.lead_filepath);
		 register=(Button)view.findViewById(R.id.lead_btnRegister);
		 
		 	Bundle args = getArguments();
	        fname.setText(args.getString("firstname"));
	        lname.setText(args.getString("lastname"));
	        email.setText(args.getString("email"));
	        mobile.setText(args.getString("mobile"));
	        landline.setText(args.getString("landline"));
	        username.setText(args.getString("username"));
	        password.setText(args.getString("password"));
	        imagename=args.getString("image");
		
	        register.setText("Edit");
		 register.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if((!fname.getText().toString().trim().equals(""))&&(!lname.getText().toString().trim().equals(""))&&(!email.getText().toString().trim().equals(""))&&(!username.getText().toString().trim().equals(""))&&(!password.getText().toString().trim().equals(""))){
				new Create().execute();
				}
				else{
					FragmentManager fm = getActivity().getSupportFragmentManager();
				     EmptyAlert alert = new EmptyAlert();
				     alert.show(fm, "Alert_Dialog");
				}
			}
		});
	
		
		return view;
    }
	@Override
	public void onStart() {
		super.onStart();
       
        
        /*register.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if((!fname.getText().toString().trim().equals(""))&&(!lname.getText().toString().trim().equals(""))&&(!email.getText().toString().trim().equals(""))&&(!username.getText().toString().trim().equals(""))&&(!password.getText().toString().trim().equals(""))){
				new Create().execute();
				}
				else{
					FragmentManager fm = getActivity().getSupportFragmentManager();
				     EmptyAlert alert = new EmptyAlert();
				     alert.show(fm, "Alert_Dialog");
				}
			}
		});*/
       
	};
	public void setUrl(String file){
		filepath.setText(file);
	}
    public void clearFields(){
    	fname.setText("");
    	lname.setText("");
    	email.setText("");
    	mobile.setText("");
    	landline.setText("");
    	username.setText("");
    	password.setText("");
    	filepath.setText("");
    	username.setText("");
    	password.setText("");
    }

	//Asynctask
	public class Create extends AsyncTask<Void, Void, Void> {

	    private final ProgressDialog dialog = new ProgressDialog(fa);
	    boolean results;

	    protected void onPreExecute() {

	            this.dialog.setMessage("Loading...");
	            this.dialog.show();
	    }


	    protected Void doInBackground(final Void... unused) {

	    	String firstname=fname.getText().toString();
	    	String lastname=lname.getText().toString();
	    	String emailAddress=email.getText().toString();
	    	String mob=mobile.getText().toString();
	    	String land=landline.getText().toString();
	    	String user=username.getText().toString();
	    	String pass=password.getText().toString();
	    	
	    	String file=filepath.getText().toString().trim();
	    	String filename=new File(file).getName();
	    	
	    	con = new FTPClient();
	    	
			try {
				con.connect(host);
				
					if(!(file.equals(""))){
						
						results = consume.EditLead(firstname, lastname, mob,land,emailAddress, LoginActivity.id,user,pass,"http://"+host+folderLocation+filename);
						
						Log.v("choya", "nesud"+firstname);
						
						if(results){
							

						if (con.login(ftpuser, ftppass)) {
					
							con.enterLocalPassiveMode(); // important!
							con.setFileType(FTP.BINARY_FILE_TYPE);

							BufferedInputStream buffIn = null;
							buffIn = new BufferedInputStream(
							new FileInputStream(new File(file)),8192);

							boolean result = con.storeFile(saveLocation
							+ folderLocation + filename, buffIn);
							buffIn.close();
							if (result)
								Log.v("upload result", "succeeded");
							con.logout();
							con.disconnect();
							
							
							}
						 
						}
						
						 
						
					}
					else
					{
					results = consume.EditLead(firstname, lastname, mob,land,emailAddress, LoginActivity.id,user,pass,imagename);
					}
				
			} catch (Exception e) {
				Log.e("","",e);
			}
	   
	        return null; 
	    }
	    
	    protected void onPostExecute(Void result) {
	    	
	    	if(results){
	    		FragmentManager fm = getActivity().getSupportFragmentManager();
	             AlertDialogFragment alert = new AlertDialogFragment();
	             alert.show(fm, "Alert_Dialog");
	             clearFields();
	             fm.popBackStack();
	    	}
	    	else{
	    		 FragmentManager fm = getActivity().getSupportFragmentManager();
			     ErrorAlert alert = new ErrorAlert();
			     alert.show(fm, "Alert_Dialog");
	    	}
	    	
	    	if (this.dialog!=null) {
	            this.dialog.dismiss();
	            }  
	     }
	}
}
